# -*- coding: utf-8 -*-
"""
Created on Tue Feb 03 17:01:28 2015

The file takes input as aircraft name and runs full analysis:
- aero
- mission
- performance
- stability and control

and generates text report


@author: Maxim
"""

import aircraft_FW
import db_tools
from performance import point_performance_analysis
from perf_field import field_performance_analysis
import datetime
from mission import run_mission_B11, run_maximum_endurance
import matplotlib.pyplot as plt


def get_mission_analysis_report(ac):
    """
    - display mission profile
    - mission fuel
    """
    ac.get_mission_fuel()
    out = ac.mission._report()
    return out

def display_text_contents(path):
    fid = open(path,'rt')
    print fid.read()
    fid.close()

def get_weight_report(ac,detailedReport):
    if detailedReport:
        output = ac.mass.total._report()
    else:
        output = ac.mass._report_basic()
    return output


def get_stability_report(ac):
    ac.get_aero_single_point()
    output = ac.aeroResults._report()
    return output    

def get_performance_report(ac,detailedReport):
    output = point_performance_analysis(ac,detailedReport=detailedReport)
    return output

def get_max_range(ac):
    R = run_mission_B11(ac)
    out = '{:<23} = {:<12.1f}{:<6}\n'.format('Maximum range',R/1e3,'km')
    return out

def get_max_endurance(ac):
    E = run_maximum_endurance(ac)
    out = '{:<23} = {:<12.4f}{:<6}\n'.format('Maximum endurance',E/3600,'hr')
    return out

def get_flight_envelope(ac,fuelFraction=0.5):
    pass


def run_integrated_analysis(name=None, outputFile=None,detailedReport=True):
    if name==None:
        name = 'X47B'
        #name = str(raw_input('Enter aircraft name:\n'))
    if outputFile==None:
        outputFile = 'analysisReport_detailed.txt'
        #outputFile = str(raw_input('Enter output report textfile name:\n'))
    
    # initialize aircraft and report output
    report = db_tools.TextReport()
    report.write_line('%s aircraft integrated analysis output'%name)
    report.write_Header(str(datetime.datetime.now()))
    report.write_newline(2)

    ac = aircraft_FW.load(name)

    # mission fuel
    report.write_header_in_quotes('mission fuel')
    reportMis = get_mission_analysis_report(ac)
    report.write(reportMis)
    report.write_newline(2)

    # weight and balance
    report.write_header_in_quotes('weight and balance')
    reportWB = get_weight_report(ac,detailedReport)
    report.write(reportWB)
    report.write_newline(2)
    
    report.write_header_in_quotes('stability and control')
    reportSnC = get_stability_report(ac)
    report.write(reportSnC)
    report.write_newline(2)
    
    report.write_header_in_quotes('performance')
    reportPerf  = get_performance_report(ac,detailedReport)
    reportPerf2 = get_max_range(ac)
    reportPerf3 = get_max_endurance(ac)

    report.write(reportPerf)
    report.write('\nMISSION PERFORMANCE\n'+'='*40+'\n')
    report.write(reportPerf2)
    report.write(reportPerf3)
    
    report.write('\nFIELD PERFORMANCE\n'+'='*40+'\n')
    reportField = field_performance_analysis(ac,detailedReport)
    report.write(reportField)
    
    report.save_to_file(outputFile,'wt')

    display_text_contents(outputFile)


if __name__=="__main__":
    run_integrated_analysis()
